package com.zixue.heima.helloNetty.EventLoopGroopDemo;

import io.netty.channel.DefaultEventLoop;
import io.netty.channel.EventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.DefaultPromise;
import io.netty.util.concurrent.Promise;

import java.util.concurrent.ExecutionException;

/**
 * @ClassName : NettyPromiseDemo
 * @Description : TODO
 * @Author : chunxiong
 * @Date : 2025/5/8 23:29
 * @Version 1.0
 */
public class NettyPromiseDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //1。准备eventLoop对象
        EventLoop eventExecutors = new NioEventLoopGroup().next(); //这里要。next 不然没结果
        //2。可以主动创建promise ，结果容器， 其他线程往里面填充结果
        Promise<Integer> promise = new DefaultPromise<>(eventExecutors);
        new Thread(()->{
            System.out.println("---开始计算");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            promise.setSuccess(20);
            //也可以填充异常
        }).start(); //线程启动要start
        //结果结果 get addlinseter 都可以
        System.out.println("---等待结果");
        Integer integer = promise.get();
        System.out.println("拿到结果："+integer);

    }
}
